(function () {
    var $tbody = $("#list-body"),
    	$searchForm = $("#search-form"),
    	$feedbackDate = $("#feedbackdate"),
        trHtml = undefined,
        stations = [],
        curPage = 1;
    var _this = {
    	// 表格初始化 
        list : function(params){
        	params = params || {};
//          params = $searchForm.serializeObject();
            params.page = curPage;
            params.size=10;
            App.ajax({
                data: params,
                resUrl: "ygxStudent/list",		// 分页查询接口
                callback: function (rb) {
                    if(!rb.status) return;
                    var rbData = rb.data;
                    $tbody.empty();
                    $.each(rbData.list||[], function(i, d){
                        var $tr = $(trHtml);
                        if( rbData.pageNumber == 1 ){
                        	d.num = i + 1;
                        }else{
                        	d.num = (rbData.pageNumber - 1) * 10 + i + 1;
                        }
                        $tr.setHtml(d).data(d);
                        $tr.find("button").data("id", d.id);
                        _this.initStatus($tr, d.lineType);
//                      _this.initBtn($tr, d);
                        $tbody.append($tr);
                    });

                    require(['laypage'], function (laypage) {
                        laypage({
                            cont: 'page-body', //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：<div id="page1"></div>
                            pages:rbData.totalPage, //通过后台拿到的总页数
                            curr: rbData.pageNumber || 1, //当前页
                            jump: function (obj, first) { //触发分页后的回调
                                curPage = obj.curr;
                                if (!first) {
                                    _this.list();
                                }
                            }
                        });
                    });
                }
            });
        },
        // 事件初始化
        initEvent : function(){
        	// 日期初始化
//      	$("#startDate").dateRangePicker(
//				{
//					autoClose: false,
//					singleDate: true,
//					timePicker : false,
//					startOfWeek: 'Wednesday',
//					format:'YYYY-MM-DD',
//					time: {
//	                    enabled: false
//	               },
//				}
//			);
//			$("#endDate").dateRangePicker(
//				{
//					autoClose: false,
//					singleDate: true,
//					timePicker : false,
//					startOfWeek: 'Wednesday',
//					format:'YYYY-MM-DD',
//					time: {
//	                    enabled: false
//	               },
//				}
//			);
			$("#startDate").dateRangePicker({
				autoClose: false,
				singleDate: true,
				timePicker: false,
				startOfWeek: 'Wednesday',
				format: 'YYYY-MM-DD',
				time: {
					enabled: false 
				}, 
				callback: function(){
               		if( _this.dayDiff( $("#startDate").val().split(' ')[0] , $("#endDate").val().split(' ')[0] ) <= 0 ){
               			$("#endDate").val('');
               		}
               		if( $("#startDate").val().split(' ')[0] != '' ){
	               		var startTime = $("#startDate").val().split(' ')[0];
	               		$("#endDate").dateRangePicker(
							{
								startDate: startTime,
			    				endDate: '2020-12-31',
								autoClose: false,
								singleDate: true,
								timePicker : false,
								startOfWeek: 'Wednesday',
								format:'YYYY-MM-DD',
								time: {
				                    enabled: false
				               },
				               callback: function(){
				               }
							}
						);
               		}
               	}
			});
			$("#endDate").dateRangePicker(
				{
					autoClose: false,
					singleDate: true,
					timePicker : false,
					startOfWeek: 'Wednesday',
					format:'YYYY-MM-DD',
					time: {
	                    enabled: false
	               	},
	               	callback: function(){
	                }
				}
			);
        	// 查询初始化
            $searchForm.submit(function(){
            	var params = $searchForm.serializeObject();
            	var feedbackDate = $feedbackDate.val()||"";
				if(feedbackDate.indexOf("to")!=-1){
					params.beginDt = feedbackDate.split(" to ")[0];
					params.endDt = feedbackDate.split(" to ")[1];
				}
				if(!params.beginDt || !params.endDt){
					layer.msg("请正确选择反馈日期区间", { icon: 2 });
					return;
				}
				curPage = 1;
				_this.list(params);
				return false;
			});
			// 回复弹窗
            $("#list-body").delegate("[btn-type=reply_btn]",'click',function(){
            	var id = $(this).parents('tr').data('id');
                var msg = '<div class="col-md-12" style="padding:20px 50px 0 20px">'
						+ '<div class="form-group">'
						+ '<label class="control-label col-md-2" style="text-align: right;color:#666;">回复内容</label>'
						+ '<div class="input-group col-md-10">'
						+ '<textarea maxlength="200" type="text" rows="3" class="form-control" id="content" name="content"></textarea>'
						+ '</div>'
						+ '</div>'
						+ '</div>';
            	var _index = layer.confirm( msg, {
					type:1,
					title:"回复反馈",
					area: ['600px', 'auto'], //宽高
					btn: ['确认', '取消']
				}, function(){
                        // 确定点击后的操作
                        App.ajax({
                        	data:{id: id},
                        	resUrl: 'ygxStudent/delete',
                        	callback: function(rb){
                        		if (!rb.status) {
                                    layer.msg(rb.msg, { icon: 5 });
                                    return;
                                }else{
                                	layer.msg(rb.msg, { icon: 5 });
                                	_this.list();
			                        layer.close(_index);
                                }
                        	}
                        });
				}, function(){
					// 取消
					layer.close(_index);
				});
          	});
          	// 查看线路详情窗口
			App.delegate({
                btntype: "[btn-type=details]",
                callback: function (data) {
                    var opt = {
                        content: $("#dform").html(),
                        submitBtn: "",
                        submit: function (from) {
                            
                        },
                        callback: function (e) {
                        	$("#entitySlider .detail-brand").find("vl").html("反馈详情");
                        	// 打开后的数据处理
                        	var $form = $(e);
							App.setFormData($form, data);
                        }
                    };
                    App.drawDetailPanel(opt);
                }
            });
        },
        /**
		 * 两个日期的天数差，参数格式：'YYYY-MM-DD HH:mm:ss'
		 * @param {Object} startDate 开始日期	
		 * @param {Object} endDate 结束日期
		 */
		dayDiff: function( startDate, endDate ){
			var beginTime = startDate;
			var endTime = endDate;
			var beginDate = new Date(beginTime);
			var endDate = new Date(endTime);
			var day = (endDate.getTime() - beginDate.getTime() ) / ( 24*60*60*1000 ) + 1;
			return day;
		},
        // 处理状态初始化
        initStatus : function($tr, status){
            if(status == 2){
                $tr.find("[html-lineType]").html('<span class="label label-info">返校</span>');
            }else if(status == 1){
                $tr.find("[html-lineType]").html('<span class="label label-success">离校</span>');
            }
        },
        // 按钮定义初始化
        initBtn : function($tr, data){
           	if( data.no == null ){
           		var vStr = '<button class="am-btn am-btn-default am-btn-xs am-text-secondary" btn-type="vehicle"  ><span class="fa fa-cogs"></span> 分配车辆</button>';
           	}else{
                var vStr = '<button class="am-btn am-btn-default am-btn-xs am-text-secondary" btn-type="cancelVehicle"  ><span class="am-icon-pencil-square-o"></span> 取消车辆</button>';
           	}
           	
           	if( data.driverName == null ){
           		var dStr = '<button class="am-btn am-btn-default am-btn-xs am-text-secondary" btn-type="driver"  ><span class="fa fa-cogs"></span> 分配司机</button>';
           	}else{
                var dStr = '<button class="am-btn am-btn-default am-btn-xs am-text-secondary" btn-type="cancelDriver"  ><span class="am-icon-pencil-square-o"></span> 取消司机</button>';
           	}
           	var fixedStr = '<button class="am-btn am-btn-default am-btn-xs am-text-secondary" btn-type="vehiclePos"  ><span class="fa fa-map-marker"></span> 车辆实时位置</button>';
			$tr.find('.am-btn-group').html(vStr + dStr +fixedStr);
        },
        init : function () {
            trHtml = $tbody.html();
            $tbody.empty().show();
            _this.initEvent();
            _this.list();
        }
    }
    _this.init(); //页面初始化入口设置
})();
